package cep;

import robocode.Robot;
import robocode.*;
import java.awt.Color;

public class Bot1 extends Robot {
	int count = 0; // Keeps track of how long we've
	// been searching for our target
	double gunTurnAmt; // How much to turn our gun when searching
	String trackName; // Name of the robot we're currently tracking

	//boolean peek; // Don't turn if there's a robot there
	double moveAmount; // How much to move	
	boolean peek = false;
	int countOthers = 500;

	/**
	 * run: MyRobot1's default behavior
	 */
	public void run() {
		countOthers = getOthers();
		setColors(Color.red, Color.blue, Color.green);

		// Move to wall
		// Initialize moveAmount to the maximum possible for this battlefield.
		moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
		// turnLeft to face a wall.
		// getHeading() % 90 means the remainder of 
		// getHeading() divided by 90.
		turnLeft(getHeading() % 90);
		ahead(moveAmount);
		// Turn the gun to turn right 90 degrees.
		peek = true;
		turnGunRight(90);
		turnRight(90);

		setAdjustGunForRobotTurn(false); // Keep the gun still when we turn
		gunTurnAmt = 10; // Initialize gunTurn to 10				

		// Loop forever
		while (true) {
			countOthers = getOthers();

			if (countOthers > 5) {
				//setAdjustGunForRobotTurn(false);
				// Look before we turn when ahead() completes.
				peek = true;
				// Move up the wall
				ahead(moveAmount);
				// Don't look now
				peek = false;
				// Turn to the next wall
				turnRight(90);
			}

			else {

				setAdjustGunForRobotTurn(true); // Keep the gun still when we turn	

				// turn the Gun (looks for enemy)
				turnGunRight(gunTurnAmt);
				// Keep track of how long we've been looking
				count++;

				// If we haven't seen our target after 10 turns, find another target
				if (count > 11) {
					trackName = null;
				}
				// If we haven't seen our target for 5 turns, look right
				else if (count > 5) {
					gunTurnAmt = 10;
				}
				// If we've haven't seen our target for 2 turns, look left
				else if (count > 2) {
					gunTurnAmt = -10;
				}

			}

		}
	}

	/**
	 * onScannedRobot: What to do when you see another robot
	 */
	public void onScannedRobot(ScannedRobotEvent e) {

		if (countOthers > 5) {
			if (getGunHeat() == 0) {
				fire(2);
			}
			// Note that scan is called automatically when the robot is moving.
			// By calling it manually here, we make sure we generate another scan event if there's a robot on the next 
			// wall, so that we do not start moving up it until it's gone.
			if (peek) {
				scan();
			}
		} else {
			// If we have a target, and this isn't it, return immediately
			// so we can get more ScannedRobotEvents.
			if (trackName != null && !e.getName().equals(trackName)) {
				return;
			}

			// If we don't have a target, well, now we do!
			if (trackName == null) {
				trackName = e.getName();
				out.println("Tracking " + trackName);
			}
			// This is our target.  Reset count (see the run method)
			count = 0;
			// If our target is too far away, turn and move toward it.
			if (e.getDistance() > 150) {
				gunTurnAmt = normalRelativeAngle(e.getBearing()
						+ (getHeading() - getRadarHeading()));

				turnGunRight(gunTurnAmt);
				turnRight(e.getBearing());
				ahead(e.getDistance() - 140);
				return;
			}

			// Our target is close.
			gunTurnAmt = normalRelativeAngle(e.getBearing()
					+ (getHeading() - getRadarHeading()));
			turnGunRight(gunTurnAmt);
			if (e.getEnergy() > 16) {
				if (getGunHeat() == 0) {
					fire(3);
				}
			} else {
				if (getGunHeat() == 0) {
					fire(2);
				}
			}

			// Our target is too close!  Back up.
			if (e.getDistance() < 100) {
				if (e.getBearing() > -90 && e.getBearing() <= 90) {
					back(40);
				} else {
					ahead(40);
				}
			}
			scan();
		}
	}

	/**
	 * onHitByBullet: What to do when you're hit by a bullet
	 */
	public void onHitByBullet(HitByBulletEvent e) {
		if (countOthers > 5) {
		} else {
			if (trackName == null) {
				gunTurnAmt = normalRelativeAngle(e.getBearing()
						+ (getHeading() - getRadarHeading()));
				turnGunRight(gunTurnAmt);
				scan();
			}
			;
		}
	}

	/**
	 * normalRelativeAngle:  Returns angle such that -180 < angle <= 180
	 */
	public double normalRelativeAngle(double angle) {
		if (angle > -180 && angle <= 180) {
			return angle;
		}
		double fixedAngle = angle;

		while (fixedAngle <= -180) {
			fixedAngle += 360;
		}
		while (fixedAngle > 180) {
			fixedAngle -= 360;
		}
		return fixedAngle;
	}

	public void onHitRobot(HitRobotEvent e) {

		if (countOthers > 5) {
			// If he's in front of us, set back up a bit.
			if (e.getBearing() > -90 && e.getBearing() < 90) {
				back(100);
			} // else he's in back of us, so set ahead a bit.
			else {
				ahead(100);
			}
		} else {
			gunTurnAmt = normalRelativeAngle(e.getBearing()
					+ (getHeading() - getRadarHeading()));
			turnGunRight(gunTurnAmt);
			scan();
		}

	}

}
